home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 3792 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: nntp.teleport.com!sschaem
  2. From: sschaem@teleport.com (Stephan Schaem)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: x ^= y ^= x ^= y;
  5. Date: 24 Feb 1996 15:47:17 GMT
  6. Organization: Teleport - Portland's Public Access (503) 220-1016
  7. Message-ID: <4gnbu5$3n2@maureen.teleport.com>
  8. References: <1286.6624T1439T237@cs.ruu.nl>
  9. NNTP-Posting-Host: kelly.teleport.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Wessel Dankers (wsldanke@cs.ruu.nl) wrote:
  13. : In one of my college-books I found this transition:
  14.  
  15. : <x := x+y ; y := x-y ; x := x-y, s> -> s{s(y)/x}{s(x)/y}
  16.  
  17. : proven correct.
  18.  
  19. : In plain C:
  20.  
  21. :  x = x + y;
  22. :  y = x - y;
  23. :  x = x - y;
  24.  
  25. : will swap the integers x and y. Of course I immediately started `hacking'
  26. : on it, resulting in:
  27.  
  28. :  x = x ^ y;
  29. :  y = x ^ y;
  30. :  x = x ^ y;
  31.  
  32. : with the C bitwise-or operator.
  33.  
  34. : Which can of course be rewritten as:
  35.  
  36. :  x ^= y;
  37. :  y ^= x;
  38. :  x ^= y;
  39.  
  40. : Note that these algorithms do not use temporary storage and can be
  41. : generalized for large memory areas with a for-loop.
  42.  
  43. : My questions are:
  44.  
  45. :  a) are these routines efficient for integers? (probably not)
  46.  
  47.  It should translate to (If the variable are in register)
  48.  
  49.     eor.?    dx,dy
  50.     eor.?    dy,dx
  51.     eor.?    dx,dy
  52.  
  53.  But in C you shoulnd't care, actually it shouldn't be a concern
  54.  on how its implemented. you can only think on the methode, if one
  55.  particular C compiler screwup you have to accept the fact.
  56.  
  57. :  b) is this x ^= y ^= x ^= y; method usable with the blitter?
  58. :     It would be possible to swap two memory locations with no
  59. :     extra memory required in three Blit calls. Is the blitter
  60. :     equally fast with copying as with XOR'ing?
  61.  
  62.  I personaly never cared to swap chipmem, and on my
  63.  system I would use a generalized CPU function.(32bit fastmem)
  64.  
  65.     move.l    (a0),d1
  66.     move.l    (a1),(a0)+
  67.     move.l    d1,(a1)+
  68.  
  69.  equal to 4 bus access to swap 4 byte. VS 3*3*2 : 18 bus access with
  70.  the blitter... >4 time slower on large exange, ouch. (On small its
  71.  alot worse because of blitter setup)
  72.  
  73.  Stephan
  74.